//
//  JPAudioMainVC.m
//  GC_OC
//
//  Created by Kris on 2023/4/19.
//

#import "JPAudioMainVC.h"
#import "XDSegmentTitleView.h"
#import "JPAudioViewController.h"
#import "FKLoginTool.h"

@interface JPAudioMainVC () <XDSegmentTitleViewDelegate>
@property (nonatomic, strong) XDSegmentTitleView *titleView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, weak) UIButton *collectBtn;

@property (nonatomic, strong) NSArray *titleArr;
@end

@implementation JPAudioMainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.titleArr = @[@"音频", @"视频", @"字幕"];
    self.titleArr = @[@"音频"];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCollectStatus) name:@"updatePlayItem" object:nil];
    [self setupUI];
    self.titleView.hidden = YES;
    [self updateCollectStatus];
}

- (void)setupUI
{
    self.navigationController.navigationBar.hidden = YES;
    
    UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"playerBg"]];
    bg.userInteractionEnabled = true;
    bg.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"icon_fanhui"] forState:0];
    [backBtn addTarget:self action:@selector(didClickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(18);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(12);
        make.width.height.mas_equalTo(40);
    }];
    
    UILabel *classLabel = [[UILabel alloc] init];
    classLabel.text = [SUPlayerManager manager].currentSong[@"top_pid_name"];
    classLabel.textColor = [UIColor whiteColor];
    classLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
    [self.view addSubview:classLabel];
    [classLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backBtn).offset(-7);
//        make.centerY.equalTo(backBtn);
        make.left.equalTo(backBtn.mas_right).offset(2);
    }];
    
    UILabel *classLabel1 = [[UILabel alloc] init];
    classLabel1.text = [SUPlayerManager manager].currentSong[@"top_parent_pid_name"];
    classLabel1.textColor = ColorFromHexAndAlpa(0xFFFFFF, 0.7);
    classLabel1.font = [UIFont systemFontOfSize:14 weight:UIFontWeightSemibold];
    [self.view addSubview:classLabel1];
    [classLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(classLabel.mas_bottom).offset(0);
        make.left.equalTo(backBtn.mas_right).offset(2);
    }];
    
    UIButton *tapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [tapBtn addTarget:self action:@selector(didClickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tapBtn];
    [tapBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(backBtn).offset(0);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(40);
    }];
    
    UIButton *collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:collectBtn];
    self.collectBtn = collectBtn;
    [collectBtn setImage:[UIImage imageNamed:@"icon_Collect_default"] forState:UIControlStateNormal];
    [collectBtn setImage:[UIImage imageNamed:@"icon_Collect_select"] forState:UIControlStateSelected];
    [collectBtn addTarget:self action:@selector(collectBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [collectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(32);
        make.height.offset(32);
        make.centerY.equalTo(backBtn);
        make.right.offset(-10);
    }];
    
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(44);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(190);
        make.height.mas_equalTo(50);
    }];
    
    UIView *scrBgView = [[UIView alloc] init];
    scrBgView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:scrBgView];
    [scrBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.titleView.mas_bottom);
    }];
    [scrBgView layoutIfNeeded];

    
    CGFloat height = scrBgView.bounds.size.height - HY_BottomWhiteBar_Height;
    //创建ScrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
    self.scrollView.backgroundColor = [UIColor clearColor];
    [scrBgView addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(self.titleArr.count * SCREEN_WIDTH, height);
//    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.scrollEnabled = NO;
    
    
    JPAudioViewController *vc = [JPAudioViewController new];
    __weak typeof(self) weakSelf = self;
    vc.pushVC = ^(AudioViewController * viewC) {
        [weakSelf.navigationController pushViewController:viewC animated:true];
    };
    vc.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
   
    [self.scrollView addSubview:vc.view];
    [self addChildViewController:vc];
            
//    UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, height)];
//    v1.backgroundColor = [UIColor cyanColor];
//    [self.scrollView addSubview:v1];

//    UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*2, 0, SCREEN_WIDTH, height)];
//    v2.backgroundColor = [UIColor systemPinkColor];
//    [self.scrollView addSubview:v2];

}

- (void)didClickBack
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)collectBtnClicked
{
    // 先判断用户是否登录
    @weakify(self)
    [FKLoginTool checkLogin:^{
        @strongify(self)
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"media_id"] = [SUPlayerManager manager].currentSong[@"id"];
        UserModel *user = [FKCacheTool userInfo];
        param[@"user_id"] = user.userinfo.user_id;
        [FKHttpTool postWithSuffix:@"/api/collect/setCollect" param:param success:^(FKBaseResult *result) {
            if (result.isSuccess) {
                [SVProgressHUD showSuccessWithStatus:result.msg];
                if ([result.msg containsString:@"收藏成功"]) {
                    self.collectBtn.selected = true;
                } else {
                    self.collectBtn.selected = false;
                }
            }
        } failure:nil];
    }];
}

- (void)XDSegmentTitleView:(XDSegmentTitleView *)titleView startIndex:(NSInteger)startIndex endIndex:(NSInteger)endIndex
{
    [self.scrollView setContentOffset:CGPointMake(endIndex*SCREEN_WIDTH, 0)];
}


- (XDSegmentTitleView *)titleView
{
    if (!_titleView) {
        _titleView = [[XDSegmentTitleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) titles:self.titleArr delegate:self];
        _titleView.canScroll = NO;
        _titleView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_titleView];
    }
    return _titleView;
}

- (void)updateCollectStatus
{
    UserModel *user = [FKCacheTool userInfo];
    if (!user) return; // 如果没用登录，则不请求
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"media_id"] = [SUPlayerManager manager].currentSong[@"id"];
    param[@"user_id"] = user.userinfo.user_id;
    [FKHttpTool postWithSuffix:@"/api/collect/checkCollect" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            NSInteger collectStatus = [result.data[@"collect_status"] integerValue];
            self.collectBtn.selected = collectStatus == 1;
        }
    } failure:nil];
}


@end
